<template>
  <!-- 添加或修改团队对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
    <el-form ref="form" :model="form" :show-message="false" label-width="100px">
      <el-form-item label="所属单位" prop="companyId" required>
        <SelectCompany
          :defaultValue="form.companyId"
          :companyType="0"
          @change="(e) => (form.companyId = e)"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="团队名称" prop="teamName" required>
        <el-input
          v-model="form.teamName"
          placeholder="请输入团队名称"
          :maxlength="15"
        />
      </el-form-item>
      <el-form-item label="团队头像" prop="teamImg" required>
        <ImageUpload v-model="form.teamImg" :width="100" :height="100" />
      </el-form-item>
      <el-form-item label="团队码" prop="teamCode">
        <el-input v-model="form.teamCode" disabled />
      </el-form-item>
      <el-form-item label="团队简介" prop="teamProfile">
        <el-input
          v-model="form.teamProfile"
          placeholder="请输入团队简介"
          type="textarea"
          :rows="3"
          :maxlength="300"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ImageUpload from "@/components/ImageUpload";
import SelectCompany from "@/components/SelectCompany";
import { addTeam, editTeam } from "@/api/company/groups";
import { getCompany } from "@/api/company/company";

export default {
  components: { ImageUpload, SelectCompany },
  data() {
    return {
      title: "新建团队",
      open: false,
      form: {
        teamId: undefined,
        teamName: "",
        teamImg: "",
        teamProfile: "",
        companyId: "",
        companyName: "",
      },
      loading: false,
    };
  },
  methods: {
    openDialog(form) {
      this.title = "新建团队";
      Object.assign(this.$data.form, this.$options.data().form);
      this.open = true;

      if (form && form.teamId) {
        this.title = "编辑团队";
        this.form = {
          teamId: form.teamId,
          teamName: form.teamName,
          teamCode: form.teamCode || "",
          teamImg: form.teamImg,
          teamProfile: form.teamProfile,
          companyId: form.companyId,
          companyName: form.companyName,
        };
      }

      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          this.loading = true;
          try {
            const companyInfo = await getCompany(this.form.companyId);
            this.form.companyName = companyInfo.data.cname;
            let res = {};
            if (this.form.teamId) {
              res = await editTeam(this.form);
            } else {
              res = await addTeam(this.form);
            }
            this.$emit("refresh");
            this.loading = false;
            this.open = false;
            if (res.data) {
              this.$message.success("新增团队成功");
              this.$router.push({
                path: `/company/groupsDetails/index/${res.data}`,
              });
            } else {
              this.$message.success("编辑团队成功");
            }
          } catch (error) {
            this.loading = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
